package geektime.spring.springbucks.service;

import geektime.spring.springbucks.mapper.CoffeeMapper;
import geektime.spring.springbucks.model.Coffee;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@CacheConfig(cacheNames="coffe")
public class CoffeeService {
    @Autowired
    private CoffeeMapper coffeeRepository;

    @CacheEvict(beforeInvocation = true,allEntries = true)
    public  List<Coffee>  reloadCoffee1(int page,int num) {
        return coffeeRepository.findAllWithRowBounds(new RowBounds(page,num)) ;
    }
    @Cacheable
    public   List<Coffee> findAllWithRowBounds1(int page,int num) {
        return coffeeRepository.findAllWithRowBounds(new RowBounds(page,num)) ;
    }

    public   List<Coffee> findAllWithRowBounds2(String name) {

        return coffeeRepository.findAllWithRowBounds2(  name) ;
    }

    public   List<Coffee> findAllWithRowBounds3(String name) {

        return coffeeRepository.findAllWithRowBounds3(  name) ;
    }

    public   List<Coffee> findAllWithRowBounds(int page,int num) {
        return coffeeRepository.findAllWithRowBounds(new RowBounds(page,num)) ;
    }

    @CacheEvict(beforeInvocation = true,allEntries = true)
    public  List<Coffee>  reloadCoffee(int page,int num) {
        return coffeeRepository.findAllWithRowBounds(new RowBounds(page,num)) ;
    }

    public Coffee findOneCoffee(String name){
       return   coffeeRepository.findOneCoffee(name);
    }
}
